package com.yodawnla.littleWar.scene;

import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.util.MathUtils;

import android.view.KeyEvent;

import com.yodawnla.lib.YoScene;
import com.yodawnla.lib.util.widget.YoButton;
import com.yodawnla.littleWar.card.CardMgr;
import com.yodawnla.littleWar.card.Deck;
import com.yodawnla.littleWar.card.HandCard;
import com.yodawnla.littleWar.card.HeroCard;
import com.yodawnla.littleWar.player.Player;
import com.yodawnla.littleWar.system.SE;
import com.yodawnla.littleWar.system.Tex;
import com.yodawnla.littleWar.system.Values;
import com.yodawnla.littleWar.ui.NameHud;

public class TitleScene extends YoScene
{
	Sprite mTitleStartBg;
	Sprite mTitleBg;
	YoButton mBackBtn;
	YoButton mMapBtn;
	YoButton mEditBtn;
	YoButton mItemBtn;
	YoButton mFriendBtn;
	YoButton mRankingBtn;
	YoButton mGalleryBtn;
	YoButton mShopBtn;
	YoButton mEggBtn;
	YoButton mSerialBtn;
	YoButton mStartBtn;	
	
	@Override
	public void loadScene()
	{		
		CardMgr cardMgr = CardMgr.getInstance();
		Player palyer = Player.getInstance();
		Deck playerDeck = new Deck();
		HeroCard heroCard = cardMgr.createHeroCard("0!0!2!1!100!100!12!50!0");//job!id!star!lv!maxHp!hp!atk!cost!exp!
		playerDeck.setHeroCard(heroCard);
		heroCard.setPosition(15, 220);
		palyer.addHeroCard(heroCard);
		for(int i = 0; i < 10; i++)
		{
			HandCard card = cardMgr.createHandCard("s!" + Values.HandCardType.Red + "!" + MathUtils.random(1, 5));
			playerDeck.addCardToDeck(card);
			palyer.addHandCard(card);
			
			card = cardMgr.createHandCard("s!" + Values.HandCardType.Green + "!" + MathUtils.random(1, 5));
			playerDeck.addCardToDeck(card);
			palyer.addHandCard(card);
			
			card = cardMgr.createHandCard("s!" + Values.HandCardType.Blue + "!" + MathUtils.random(1, 5));
			playerDeck.addCardToDeck(card);
			palyer.addHandCard(card);
		}
		
		palyer.setDeck(playerDeck);
	}
	
	@Override
	public void onEnterScene()
	{
		setAdVisible(false);
		mTitleStartBg = new Sprite(0, 0, 480, 800, getTexture(Tex.titleStartBg));
		mScene.attachChild(mTitleStartBg);
		mTitleBg = new Sprite(0, 0, 480, 800, getTexture(Tex.titleBg));
		mTitleBg.setAlpha(0);
		mScene.attachChild(mTitleBg);
		
		mStartBtn = new YoButton(this, 101, 688, getTexture(Tex.startBtn))
		{
			@Override
			public void onClickedEvent()
			{
				setAdVisible(true);
				playSound(SE.Click);
				mTitleStartBg.registerEntityModifier(new AlphaModifier(1, 1, 0));
				mStartBtn.registerEntityModifier(new AlphaModifier(1, 1, 0));
				
				mTitleBg.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(1, 0, 0), new AlphaModifier(1, 0, 1)));
				mBackBtn.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(1, 0, 0), new AlphaModifier(1, 0, 1)));
				mMapBtn.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(1, 0, 0), new AlphaModifier(1, 0, 1)));
				mEditBtn.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(1, 0, 0), new AlphaModifier(1, 0, 1)));
				mItemBtn.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(1, 0, 0), new AlphaModifier(1, 0, 1)));
				mFriendBtn.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(1, 0, 0), new AlphaModifier(1, 0, 1)));
				mRankingBtn.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(1, 0, 0), new AlphaModifier(1, 0, 1)));
				mGalleryBtn.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(1, 0, 0), new AlphaModifier(1, 0, 1)));
				mShopBtn.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(1, 0, 0), new AlphaModifier(1, 0, 1)));
				mEggBtn.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(1, 0, 0), new AlphaModifier(1, 0, 1)));
				mSerialBtn.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(1, 0, 0), new AlphaModifier(1, 0, 1)));
				
				HeroCard hero = Player.getInstance().getDeck().getHeroCard();
				hero.setWidth(132);
				hero.setHeight(190);
				hero.setPosition(20, 70);
				hero.setAlpha(0);
				hero.showHpAndAtk(false);
				mScene.attachChild(hero);
				hero.registerEntityModifier(new SequenceEntityModifier(new AlphaModifier(1, 0, 0), new AlphaModifier(1, 0, 1)));
				
				NameHud.getInstance().show(TitleScene.this);
			}
		};
		mScene.attachChild(mStartBtn);		
		
		mBackBtn = new YoButton(this, 418, 5, getTexture(Tex.backBtn))
		{
			@Override
			public void onClickedEvent()
			{
				playSound(SE.Click);
			}
		};
		mBackBtn.setAlpha(0);
		mScene.attachChild(mBackBtn);
		
		mMapBtn = new YoButton(this, 170, 70, getTexture(Tex.mapBtn))
		{
			@Override
			public void onClickedEvent()
			{
				playSound(SE.Click);
				toScene("MapScene");
			}
		};
		mMapBtn.setAlpha(0);
		mScene.attachChild(mMapBtn);
		
		mEditBtn = new YoButton(this, 10, 280, getTexture(Tex.editBtn))
		{
			@Override
			public void onClickedEvent()
			{
				playSound(SE.Click);
				toScene("EditCardScene");
			}
		};
		mEditBtn.setAlpha(0);
		mScene.attachChild(mEditBtn);
		
		mItemBtn = new YoButton(this, 105, 280, getTexture(Tex.itemBtn))
		{
			@Override
			public void onClickedEvent()
			{
				playSound(SE.Click);
			}
		};
		mItemBtn.setAlpha(0);
		mScene.attachChild(mItemBtn);
		
		mFriendBtn = new YoButton(this, 200, 280, getTexture(Tex.friendBtn))
		{
			@Override
			public void onClickedEvent()
			{
				playSound(SE.Click);
			}
		};
		mFriendBtn.setAlpha(0);
		mScene.attachChild(mFriendBtn);
		
		mRankingBtn = new YoButton(this, 295, 280, getTexture(Tex.rankingBtn))
		{
			@Override
			public void onClickedEvent()
			{
				playSound(SE.Click);
			}
		};
		mRankingBtn.setAlpha(0);
		mScene.attachChild(mRankingBtn);
		
		mGalleryBtn = new YoButton(this, 390, 280, getTexture(Tex.galleryBtn))
		{
			@Override
			public void onClickedEvent()
			{
				playSound(SE.Click);
			}
		};
		mGalleryBtn.setAlpha(0);
		mScene.attachChild(mGalleryBtn);
		
		mShopBtn = new YoButton(this, 10, 550, getTexture(Tex.shopBtn))
		{
			@Override
			public void onClickedEvent()
			{
				playSound(SE.Click);
			}
		};
		mShopBtn.setAlpha(0);
		mScene.attachChild(mShopBtn);
		
		mEggBtn = new YoButton(this, 160, 550, getTexture(Tex.eggBtn))
		{
			@Override
			public void onClickedEvent()
			{
				playSound(SE.Click);
			}
		};
		mEggBtn.setAlpha(0);
		mScene.attachChild(mEggBtn);
		
		mSerialBtn = new YoButton(this, 336, 550, getTexture(Tex.serialBtn))
		{
			@Override
			public void onClickedEvent()
			{
				playSound(SE.Click);
			}
		};
		mSerialBtn.setAlpha(0);
		mScene.attachChild(mSerialBtn);
	}
	
	@Override
	public void onExitScene()
	{
		unloadMarkedTexturePacks();
		cleanScene();
	}
	
	@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) 
	{
		if(pKeyCode == KeyEvent.KEYCODE_BACK && pEvent.getAction() == KeyEvent.ACTION_DOWN)
		{
			//playSound("SeClick");
			return false;
		}
		return false;
	}
}